package org.idea.spring.bean.source.resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Author linhao
 * @Date created in 11:28 上午 2021/5/3
 */
@Configuration
@PropertySource(value = "META-INF/default.properties",encoding = "UTF-8")
public class ResourceInjectDemo {

    /**
     * :标示默认值
     */
    @Value("${user.id:1002}")
    private long id;

    @Value("${user.nickname:defaultName}")
    private String name;

    @Value("${user.resource:classpath://default.properties}")
    private Resource resource;


    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
        annotationConfigApplicationContext.register(ResourceInjectDemo.class);
        annotationConfigApplicationContext.refresh();
        ResourceInjectDemo resourceInjectDemo = annotationConfigApplicationContext.getBean(ResourceInjectDemo.class);
        System.out.println(resourceInjectDemo.id);
        System.out.println(resourceInjectDemo.name);
        String fileName = resourceInjectDemo.resource.getFilename();
        System.out.println(fileName);
        annotationConfigApplicationContext.close();
    }
}
